import os
import subprocess
import time
from optparse import OptionParser

testdir = 'tests\\tests\\'
answersdir = 'tests\\answers\\'
compiler_options = ''

#cmd options

parser = OptionParser()
parser.add_option('-t', '--tokenizer-only', action="store_true", dest="tokenizer", default=False, help="test the tokenizer")
parser.add_option('-e', '--expressions-parser-only', action="store_true", dest="expr_parser", default=False, help="test the expressions parser")
parser.add_option('-E', '--expressions-parser2-only', action="store_true", dest="expr_parser2", default=False, help="test the expressions parser, second ver")
parser.add_option('-p', '--ParserWithSymTable-only', action="store_true", dest="ParserWithSymTable", default=False, help="test the parser, with symbol table")
parser.add_option('-f', '--file', action="store", dest="filename", type='string', help="run specific test")

(options, args) = parser.parse_args()

if options.tokenizer:
    testdir += 'tokenizer\\'
    answersdir += 'tokenizer\\'
    compiler_options += '--tokenizer-only'
elif options.expr_parser:
    testdir += 'expressions parser\\'
    answersdir += 'expressions parser\\'
    compiler_options += '--expressions-parser-only'
elif options.expr_parser2:
    testdir += 'expressions parser2\\'
    answersdir += 'expressions parser2\\'
    compiler_options += '--expressions-parser2-only'
elif options.ParserWithSymTable:
    testdir += 'ParserWithSymTable\\'
    answersdir += 'ParserWithSymTable\\'
    compiler_options += '--ParserWithSymTable-only'
else:
    parser.print_help()
    exit()

tests = os.walk(testdir)         

for (root, dirs, files) in tests:
    if options.filename == None:
        for f in files:
            (filename, extension) = os.path.splitext(f)
            if extension.lower() != '.tst': continue;
            a = subprocess.Popen(args = 'ccompiler.exe ' + '"' + root + f + '" ' + compiler_options,
                executable = 'bin\\release\\ccompiler.exe', stdout = open(answersdir + filename + '.out', 'w'))
            t2 = t1 = time.clock()
            while a.poll() == None:
                t2 = time.clock()
                if (t2 - t1) > 5: break
            if (t2 - t1) > 5:
                print(f + ' caused a hang !!!')
                continue
            
            if os.access(answersdir + filename + '.ans', os.F_OK) != True:
                print ('There is no .ans file for ' + f)
                continue
            out = open(answersdir + filename + '.out', 'r')
            ans = open(answersdir + filename + '.ans', 'r')
            if out.read() == ans.read():
                print(f + ' passed')
            else:
                print(f + ' FAILED !!!')
    else:
        (filename, extension) = os.path.splitext(options.filename)
        if (extension.lower() != '.tst') or (os.access(testdir + options.filename, os.F_OK) != True): 
            print('bad test file' + options.filename)
            print(extension.lower())
            print(testdir + filename)
            exit()
        a = subprocess.Popen(args = 'ccompiler.exe ' + '"' + root + options.filename + '" ' + compiler_options,
            executable = 'bin\\release\\ccompiler.exe', stdout = open(answersdir + filename + '.out', 'w'))
        t2 = t1 = time.clock()
        while a.poll() == None:
            t2 = time.clock()
            if (t2 - t1) > 5: break
        if (t2 - t1) > 5:
            print (options.filename + ' caused a hang !!!')
            exit()
        ans = None
        if os.access(answersdir + filename + '.ans', os.F_OK) != True:
            print ('There is no .ans file for ' + options.filename)
        else:
            ans = open(answersdir + filename + '.ans', 'r')
        out = open(answersdir + filename + '.out', 'r')
        print ('Program output on test:')
        output = out.read()
        print (output)
        if ans != None:
            required = ans.read()
            if output == (required):
                print(options.filename + ' passed')
            else:
                print(options.filename + ' FAILED !!!')
            print('Answer on test:')
            print(required)
        break
        